<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="校区名称" prop="distributionCampusName">
        <el-input v-model="queryParams.distributionCampusName" placeholder="请输入校区名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="申请人id" prop="applicantId">
        <el-input v-model="queryParams.applicantId" placeholder="请输入申请人id" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="申请人" prop="applicantName">
        <el-input v-model="queryParams.applicantName" placeholder="请输入申请人名字" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分销审核" prop="campusIsDistribute">
        <el-select v-model="queryParams.campusIsDistribute" placeholder="请选择分销审核状态" clearable>
          <el-option v-for="dict in dict.type.sys_campus_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" disabled
          v-hasPermi="['campus:campusdistriapply:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['campus:campusdistriapply:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['campus:campusdistriapply:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['campus:campusdistriapply:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="campusdistriapplyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请分销的id" align="center" prop="applyDistributionId" />
      <el-table-column label="校区名称" align="center" prop="distributionCampusName" />
      <el-table-column label="申请人手机号" align="center" prop="demo1" />

<!--      <el-table-column label="申请人id" align="center" prop="applicantId" />-->
      <el-table-column label="申请人" align="center" prop="applicantName" />
      <el-table-column label="分销审核" align="center" prop="campusIsDistribute">

        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_campus_status" :value="scope.row.campusIsDistribute" />
        </template>
      </el-table-column>
      <el-table-column label="审核理由" align="center" prop="demo2" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['campus:campusdistriapply:edit']">修改</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['campus:campusdistriapply:remove']">删除</el-button>
          <el-link size="mini" type="success" icon="el-icon-check" @click="handleApprove(scope.row)">审批</el-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改分销校区申请列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="校区id" prop="campusId">
          <el-input v-model="form.campusId" placeholder="请输入校区id" />
        </el-form-item> -->
        <!-- 更改为下拉框选择校区 -->
        <el-form-item label="校区名称" prop="campusId">
          <el-select v-model="form.campusId" placeholder="请选择校区">
            <el-option v-for="campus in campusList" :key="campus.campusId" :label="campus.campusName"
              :value="campus.campusId"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="申请人id" prop="applicantId">
          <el-input v-model="form.applicantId" placeholder="请输入申请人id" />
        </el-form-item> -->
        <el-form-item label="申请人" prop="applicantName">
          <el-input v-model="form.applicantName" placeholder="请输入申请人名字" />
        </el-form-item>
        <el-form-item label="分销审核" prop="isDistribute">
          <el-select v-model="form.isDistribute" placeholder="请选择分销审核">
            <el-option v-for="dict in dict.type.sys_campus_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审批" :visible.sync="approveDialogVisible" width="400px">
      <el-form>
        <el-form-item label="审批结果">
          <el-select v-model="approveStatus" placeholder="请选择">
            <el-option v-for="item in approveOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审批理由">
          <el-input v-model="approveReason" placeholder="请输入审批理由"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="approveDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmApprove">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCampusdistriapply, getCampusdistriapply, delCampusdistriapply, addCampusdistriapply, updateCampusdistriapply, distribute } from "@/api/campus/campusdistriapply"
import { listCampusInfo } from "@/api/campus/campusInfo"; // 引入获取校区列表的接口

export default {
  name: "Campusdistriapply",
  dicts: ['sys_campus_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分销校区申请列表表格数据
      campusdistriapplyList: [],
      // 校区列表
      campusList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        campusId: null,
        applicantId: null,
        applicantName: null,
        isDistribute: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      approveDialogVisible: false,
      approveStatus: '',
      approveReason: '',
      currentApproveRow: null,
      approveOptions: [
        { label: '通过', value: 1 },
        { label: '不通过', value: 2 }
      ],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取校区列表
    getCampusList() {
      listCampusInfo().then((response) => {
        console.log(response);
        this.campusList = response.rows;
      });
    },
    /** 查询分销校区申请列表列表 */
    getList() {
      this.loading = true
      listCampusdistriapply(this.queryParams).then(response => {
        this.campusdistriapplyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        applyDistributionId: null,
        campusId: null,
        applicantId: null,
        applicantName: null,
        isDistribute: null,
        createdAt: null,
        updatedAt: null,
        demo1: null,
        demo2: null,
        demo3: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.applyDistributionId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getCampusList();
      this.reset()
      this.open = true
      this.title = "添加分销校区申请列表"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getCampusList();
      this.reset()
      const applyDistributionId = row.applyDistributionId || this.ids
      getCampusdistriapply(applyDistributionId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改分销校区申请列表"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.applyDistributionId != null) {
            updateCampusdistriapply(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCampusdistriapply(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const applyDistributionIds = row.applyDistributionId || this.ids
      this.$modal.confirm('是否确认删除分销校区申请列表编号为"' + applyDistributionIds + '"的数据项？').then(function () {
        return delCampusdistriapply(applyDistributionIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('campus/campusdistriapply/export', {
        ...this.queryParams
      }, `campusdistriapply_${new Date().getTime()}.xlsx`)
    },
    handleApprove(row) {
      this.currentApproveRow = row;
      this.approveStatus = '';
      this.approveReason = '';
      this.approveDialogVisible = true;
    },
    confirmApprove() {
      // console.log('选中行:', this.currentApproveRow);
      // console.log('审批状态:', this.approveStatus);
      // console.log('审批理由:', this.approveReason);

      const data = {
        applyDistributionId: this.currentApproveRow.applyDistributionId,
        campusId: this.currentApproveRow.campusId,
        isDistribute: this.approveStatus,
        demo2:  this.approveReason
      }

      distribute(data)
        .then(res => {
          console.log(res)
          this.getList()
          this.$modal.msgSuccess('修改成功')
        })

      console.log(data, 333)
      this.approveDialogVisible = false;
    },
  }
}
</script>
